import axios from 'axios';

const userRequest = axios.create({
  baseURL: import.meta.env.VITE_BASE_URL,
});
userRequest.interceptors.request.use((config) => {
  const token = localStorage.getItem('token');
  if (token) {
    // 如果 token 存在，在每个 HTTP header 都加上 token
    // Bearer 是 JWT 的认证头部信息
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});
//登录
export const loginHttp = (info) => {
  return userRequest.post('/login', {
    username: info.username,
    password: info.password,
    type: info.type,
  });
};
//注册
export const registerHttp = (info) => {
  return userRequest.post('/register', {
    username: info.username,
    password: info.password,
    type: info.type,
  });
};

//查询不同类型的用户
export const queryUserHttp = (type) => {
  return userRequest.get(`/user?type=${type}`);
};

//删除用户
export const deleteUserHttp = (info) => {
  return userRequest.post('/delete', {
    type: info.type,
    username: info.username,
  });
};
//修改用户信息
export const reviseUserData = (id, info) => {
  return userRequest.patch(`/${id}`, {
    type: info.type,
    username: info.username,
  })
}
export const changeUserPassword = (info) => {
  return userRequest.post(`/password`, {
    username: info.username,
    password: info.password,
    newName:info.newName,
    newPassword: info.newPassword,
    type: info.type
  })
}
//退出登录
export const offlineUserHttp = (info) => {
  return userRequest.post('/logout', {
    type: info.type,
    username: info.username,
  });
};
// 报告路况
const reportRequest = axios.create({
  baseURL: import.meta.env.VITE_BASE_REPORT,
});
// 获取路况信息
export const getRoadCondition = () => {
  return reportRequest.get(`/roadMessage`);
};
// 报告路况信息
export const reportRoadCondition = (payload) => {
  return reportRequest.post(`/roadMessage`, payload);
};
//删除报告路况
export const deleteRoadCondition = (id) => {
  reportRequest.delete(`/roadMessage/${id}`)
}
//修改路况信息
export const reviseRoadCondition = (id, data) => {
  reportRequest.patch(`/roadMessage/${id}`, data)
}
/* 公告的请求 */
const noticeRequest = axios.create({
  baseURL: "http://localhost:8000"
})
//发送公告
export const sendingNoticeHttp = (info) => {
  return noticeRequest.post("/notice", {
    username: info.username,
    content: info.content,
    time: info.time
  })
}
//获取全部公告
export const getNoticeHttp = () => {
  return noticeRequest.get("/notice")
}
